package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dao. Location_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;

public class LocationController extends Controller {

    private final FormFactory formFactory;
    private final  Location_Repository  location_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public LocationController(FormFactory formFactory,  Location_Repository location_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.location_Repository= location_Repository;
        this.ec = ec;
    }


    public CompletionStage<Result> getLocationByID() {
        JsonNode json = request().body().asJson();
        String applyInId = json.findPath("applyInId").textValue();
        return location_Repository.getLocationByID(applyInId).thenApplyAsync(_getList -> {
            ObjectNode result = Json.newObject();
            return ok(Json.toJson(_getList));
        }, ec.current());
    }
    public CompletionStage<Result> stopLogById() {
        JsonNode json = request().body().asJson();
        String ApplyID = json.findPath("ApplyID").textValue();

        return location_Repository.stopLogById(ApplyID).thenApplyAsync(_getList -> {
            ObjectNode result = Json.newObject();
            return ok(Json.toJson(_getList));
        }, ec.current());
    }

    public CompletionStage<Result> getGPSDatabyPoundNo() {
        JsonNode json = request().body().asJson();
        String poundNo = json.findPath("poundNo").textValue();
        String dataType = json.findPath("dataType").textValue();

        return location_Repository.getGPSDatabyPoundNo(poundNo, dataType).thenApplyAsync(_getList -> {
            ObjectNode result = Json.newObject();
            return ok(Json.toJson(_getList));
        }, ec.current());
    }
}
